
/**
 * 异步方式请求URL
 * @param {*} url 要请求的url
 * @param {*} onReturn200Callback 
 * @param {*} timeout_ms 
 */
 function getUrlAsyn(url, onReturn200Callback, timeout_ms=2000) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            if(xhr.status == 200) {
                window.localStorage.setItem(url, xhr.responseText);
                onReturn200Callback(xhr.responseText);
            }
        }
    }
    xhr.onerror = function() {
        alert("error");
        var history_response = window.localStorage.getItem(url);
        if(hishistory_response != null) {
            onReturn200Callback(history_response);
        }
    }
    xhr.timeout = timeout_ms;
    xhr.send();
}

/**
 * 异步方式请求URL，用于失败时需要向用户明确提示的场景
 * @param {*} url           要请求的url
 * @param {*} call_back     请求结束后的回调函数，参数为一个字典，内容为{status, response}
 * @param {*} parse_json    是否对response进行json解析
 * @param {*} timeout_ms    超时时间，单位：毫秒，默认2000
 */
function getUrlAsyn2(url, call_back, parse_json=true, timeout_ms=2000) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            call_back_arg = {'status':xhr.status};
            if(xhr.status == 200) {
                call_back_arg['response'] = xhr.responseText;
                if(parse_json == true) {
                    call_back_arg['response'] = JSON.parse(call_back_arg['response']);
                }
                call_back(call_back_arg);
            }
        }
    }
    xhr.ontimeout = function() {
        call_back_arg = {'status':999}
        call_back(call_back_arg);
    }
    xhr.timeout = timeout_ms;
    xhr.send();
}
